//
//  DDMessageViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/3/13.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDMessageViewController.h"
#import "DDMessageMainCell.h"
#import "DDMessageNotificationCell.h"
#import "DDChatMessageViewController.h"
#import "DDNotificationMessageViewController.h"

@interface DDMessageViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) UITableView *messageMainTableView;

@end

@implementation DDMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //[self setSubviews];
    
    //[self setConstraints];
}

- (void)viewDidLayoutSubviews
{
    if ([self.messageMainTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.messageMainTableView setLayoutMargins:UIEdgeInsetsMake(0,70,0,0)];
    }
}

- (void)setSubviews
{
    self.view.backgroundColor = COLOR_BACKGROUND;
    
    self.messageMainTableView = [[UITableView alloc] init];
    [self.messageMainTableView setBackgroundView:nil];
    [self.messageMainTableView setBackgroundColor:[UIColor clearColor]];
    [self.messageMainTableView setDelegate:self];
    [self.messageMainTableView setDataSource:self];
    self.messageMainTableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_messageMainTableView];
}

- (void)setConstraints
{
    
    WEAKSELF
    [self.messageMainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    
//    ESWeakSelf
//    [self.messageMainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        ESStrongSelf
//        make.edges.equalTo(_self.view);
//    }];
}

#pragma mark --- UITableViewDataSource Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    static NSString *cellNotificationIndentifier = @"notificationCell";
    if (indexPath.section == 0) {
        DDMessageNotificationCell *notificationCell = [tableView dequeueReusableCellWithIdentifier:cellNotificationIndentifier];
        if (!notificationCell) {
            notificationCell = [[DDMessageNotificationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellNotificationIndentifier];
        }
        return notificationCell;
    }
    DDMessageMainCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[DDMessageMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"message_icon_defualt"]];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 38)];
    headerView.backgroundColor = COLOR_BACKGROUND;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(11, 0, headerView.width, headerView.height)];
    label.text = @"最近联系的Boss";
    label.font = [UIFont systemFontOfSize:10.f];
    [headerView addSubview:label];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 38.f;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 69.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        DDNotificationMessageViewController *notificationViewController = [[DDNotificationMessageViewController alloc] init];
        notificationViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:notificationViewController animated:YES];
    } else {
        DDChatMessageViewController *chatMessageViewController = [[DDChatMessageViewController alloc] init];
        chatMessageViewController.hidesBottomBarWhenPushed = YES;
        chatMessageViewController.conversationType = ConversationType_PRIVATE;
        chatMessageViewController.targetId = @"111";
        chatMessageViewController.title = @"彭林香";
        [self.navigationController pushViewController:chatMessageViewController animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
